package com.example.studyplanning.controller;


import com.example.studyplanning.entity.Tips;
import com.example.studyplanning.services.TipsServices;
import com.example.studyplanning.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("tips")
public class TipsController {
    @Autowired
    private TipsServices tipsServices;

    //展示广场所有公开心得信息
    @GetMapping("/lists")
    public Result FindAllTips(@RequestParam(value="tipsTitle",defaultValue = "") String tipsTitle){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(tipsServices.FindAllTips(tipsTitle));
        int a=0;
        return result;
    };
    //展示详细心得
    @GetMapping("/list")
    public Result findAllTips(@RequestParam("tipsID")Integer tipsID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(tipsServices.findAllTips(tipsID));
        return result;
    }

    //添加心得
    @PostMapping("/add")
    public Result insertTips(@RequestBody Tips tips){
        Result result = new Result();
        int i = tipsServices.insertTips(tips);
        if(i==1){
            result.setCode(200);
            result.setMessage("添加成功");
        }else {
            result.setCode(400);
            result.setMessage("添加失败");
        }
        return result;
    }

    //更新心得表点赞数
    @PostMapping("/update")
    public Result UpdateTipsLike(@RequestBody Tips tips){
        Result result = new Result();
        int i = tipsServices.UpdateTipsLike(tips);
        if(i==1){
            result.setCode(200);
            result.setMessage("点赞成功");
        }else {
            result.setCode(400);
            result.setMessage("点赞失败");
        }
        return result;
    }

    //撤销点赞数
    @PostMapping("/updatelike")
    public Result UpdateLike(@RequestBody Tips tips){
        Result result = new Result();
        int i = tipsServices.UpdateLike(tips);
        if(i==1){
            result.setCode(200);
            result.setMessage("撤销成功");
        }else {
            result.setCode(400);
            result.setMessage("撤销失败");
        }
        return result;
    }

    //根据心得ID得到点赞数
    @GetMapping("/likes")
    public Result SelectLikes(@RequestParam("tipsID") Integer tipsID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(tipsServices.SelectLikes(tipsID));
        return result;
    }

    //5.3 根据用户ID得到心得信息
    @GetMapping("/getlistbyID")
    public Result SelectTipsByID(@RequestParam("userID") Integer userID){
        Result result = new Result();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setObject(tipsServices.SelectTipsByID(userID));
        return result;
    };

    //删除所选心得
    @GetMapping("/delete")
    public Result DelByID(@RequestParam("tipsID") Integer tipsID){
        Result result = new Result();
        int i = tipsServices.DelByID(tipsID);
        if (i==1){
            result.setCode(200);
            result.setMessage("删除成功");
        }else {
            result.setCode(400);
            result.setMessage("删除失败");
        }
        return result;
    };

    //tipsQuery多条件查询任务
    @GetMapping("/getlist")
    public Result getAllTips(@RequestParam(value = "date_time",defaultValue ="") String date_time,
                                 @RequestParam(value = "tipsID",defaultValue = "") Integer tipsID,
                                 @RequestParam(value = "tipsTitle",defaultValue = "") String tipsTitle,
                                 @RequestParam(value = "tipsType",defaultValue = "") Integer tipsType,
                                 @RequestParam(value = "tipsContent",defaultValue = "") String tipsContent,
                                 @RequestParam(value = "userName",defaultValue = "") String userName)
    {
        Result result = new Result();
        if (date_time.equals("")){
            result.setCode(200);
            result.setMessage("查询成功");
            result.setObject(tipsServices.SelectTheTips(tipsID,tipsTitle,tipsType,tipsContent,userName));
        }else {
            result.setCode(200);
            result.setMessage("查询成功");
            result.setObject(tipsServices.SelectTips(date_time,tipsID,tipsTitle,tipsType,tipsContent,userName));
        }
        return result;
    }

    //辅助tipsQuery多条件查询
    @GetMapping("/getlists")
    public List<Tips> getAllTheTips(
                                 @RequestParam(value = "tipsID",defaultValue = "") Integer tipsID,
                                 @RequestParam(value = "tipsTitle",defaultValue = "") String tipsTitle,
                                 @RequestParam(value = "tipsType",defaultValue = "") Integer tipsType,
                                 @RequestParam(value = "tipsContent",defaultValue = "") String tipsContent,
                                 @RequestParam(value = "userName",defaultValue = "") String userName) {
        List<Tips> list = tipsServices.SelectTheTips(tipsID, tipsTitle, tipsType, tipsContent, userName);
        return list;
    }
    @GetMapping("/all")
    public List<Tips>getDayTip(){
        List<Tips>list=tipsServices.CountDayTip();
        return list;
    }
}

